<?php
if (! defined ( 'BASEPATH' ))
    show_404 ( 'No direct script access allowed' );

class User extends Controller {
    public function __construct() {
        parent::__construct ();
        $this->lang->load ( 'myuser' );
        $this->lang->load ( 'user' );
        $this->load->module_config ( 'myuser', 'user' );
        isController ( 'myuser' );
        $this->_data ['menuon'] = 'user';
    }
    protected function _view($template, $data = array(), $result = false) {
        if ($data && is_array ( $data )) {
            return $this->load->view ( $template, $data, $result );
        }
        return $this->load->view ( $template, $this->_data, $result );
    }
    
    function index() {
        redirect('myuser/user/view');
    }
    
	protected function _executeView() {
        $array = $this->uri->uri_to_assoc ( 3, array ('id', 'action', 'groupId', 'letter', 'userStatus' ) );
        // delete action
        if (isAccess('myuser','user','delete') && $array ['action'] == 'delete' && $array ['id']) {
        	$data = array();
        	$data['userStatus'] = '2';
    
            if ($this->user_model->update($array['id'], $data)) {
                $this->_data['errorMessage'] =  'Berhasil Delete User' ;
                $this->_data['isSuccess'] =  true ;
                
                header("refresh: 1; ".site_url('myuser/user/view/groupId/'.$array ['groupId'].'/userStatus/'.$array ['userStatus'].'/page/0' ) );
            } else {
                $this->_data['errorMessage'] =  'Gagal Delete User' ;
            }
        }
    }
    
	public function view() {
    	if (! isAccess ( 'myuser', 'user', 'view'))
            redirect ();
        
            
        add_title ( $this->lang->line ( 'myuser_user_view_title' ) );
        $this->load->model ( 'user_model' );
        $this->load->model ( 'group_model' );
            
        $array = $this->uri->uri_to_assoc ( 3, array ('groupId' , 'userStatus' , 'searchFrom', 'keyword', 'limit', 'sort', 'order', 'page', 'action' ) );
        $array['page'] = (int) $array['page'];
      
        $this->_data['listGroup'] = $this->group_model->groupList ();
            
        $listStatus = $this->config->item('status');
        array_push($listStatus, 'Active (Login)');
        
        $this->_data['listStatus'] = $listStatus;
        //End
        
		//Search
		$this->load->module_helper('myuser_helper');
        $searchTipe = array('userLogin'=>'Login','userName'=>'Nama');
        $this->_data['searchTipe'] = $searchTipe;
        
        if ($this->input->post('searchFrom') && ($k = $this->input->post('keyword')) && !empty($k)) {
        	$array['action'] = 'search';
        	$array['searchFrom'] = $this->input->post('searchFrom');
        	$array['keyword'] = myUrlEncode($this->input->post('keyword'));
        }

        //Uri
        $array['groupId'] = $array['groupId'] ? $array['groupId'] : 0;
        $array['limit']	 = !empty($array['limit']) ? $array['limit'] : $this->config->item('myuser_limit');
        $array['userStatus'] = $array['userStatus'] ? $array['userStatus'] : 0;
        $array['searchFrom'] = !empty($array['searchFrom']) ? $array['searchFrom'] : 'userLogin';
        $array['keyword'] = !empty($array['keyword']) ? $array['keyword'] : 'all';
        //End
        
        //Where Parameter Filter	
        $where = array();
		if( !empty($array['keyword']) && $array['keyword'] != 'all' ) {
       		$where['searchFrom'] = $array['searchFrom']; 
       		$where['keyword'] = myUrlDecode($array['keyword']);	 
       	}
        if (!empty($array['groupId']) || $array['groupId'] != 0)
            $where['groupId'] = $array['groupId'];
		if (!empty($array['userStatus']) || $array['userStatus'] != 0)
            $where['userStatus'] = $array['userStatus'] == '2' ? '0' : $array['userStatus'];
        //End Parameter
    
        //set sort dan order
        $list_sort = array ('userLogin', 'userId', 'userName', 'userStatus' );
        $list_order = array ('asc', 'desc' );
        if (in_array ( $array ['sort'], $list_sort ))
            $this->config->set_item ( 'user_sort', $array ['sort'] );
        else
            $array ['sort'] = $this->config->item ( 'user_sort' );
        if (in_array ( $array ['order'], $list_order ))
            $this->config->set_item ( 'user_order', $array ['order'] );
        else
            $array ['order'] = $this->config->item ( 'user_order' );
        
        //page
        $page = ($array ['page'] === false) ? 0 : $array ['page'];
        $page = ( int ) $page;
        $array ['page'] = $page;
        $this->_data ['page'] = $page;

        //Url Keyword Search
        $this->_data['getUrlKeyword'] = !empty($array['keyword']) && $array['keyword'] != 'all' ? $array['keyword'] : 'all';
        
        //Limit
        $list_limit = $this->config->item('list_limit');
        $this->_data['list_limit'] = $list_limit; 

        $set_limit = $this->config->item('myuser_limit');
        $limit = !empty($array['limit']) ? $array['limit'] : $set_limit;
        $limit = !in_array($limit, $list_limit) ? $set_limit :  $limit;	   
        
        $this->load->library('pagination');
        $total = $this->user_model->getCount ($where);
        $this->_data['totalRow'] = $total;
        
        if (($array['page'] + 1) > $total)
            $array['page'] = 0;
            
        $this->_data['limit'] = $array['page'] + 1;     
        $config = $this->config->item('paging');
		$config['base_url'] = site_url('myuser/user/view/groupId/'.$array['groupId'].'/userStatus/'.$array['userStatus'].'/searchFrom/'.$array['searchFrom'].'/keyword/'.$array['keyword'].'/limit/'.$array['limit'].'/sort/'.$array ['sort'].'/order/'.$array ['order'].'/page/');
        $config['uri_segment'] = 18;
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        
        $this->pagination->initialize($config);
        $this->_data['paging'] =  $this->pagination->create_links();
		
        $dataView = $this->user_model->getList ( $where , $limit , $array['page'] , $array['sort'] , $array['order'] );
        $this->_data['dataView'] = $dataView;
        $this->_data['uri_to_assoc'] = $array;
		
        if ( empty($dataView) )
        	$this->_data['errorMessage'] = 'Maaf Data Tidak Ditemukan';
        	
        $this->_executeView();	
        $this->_view ( 'myuser/userView' );
    }
    
    public function add() {
        if (! isAccess ( 'myuser', 'user', 'add' )) {
            redirect ( 'myuser/user/view' );
        }
        
        add_js_plugin ( 'jquery.password.min.js' );
        add_title ( $this->lang->line ( 'myuser_user_add_title' ) );
        $this->_addExecute ();
        //load status
        //$this->load->model('status_model');
        $this->_data ['statusRadios'] = array ('1' => $this->lang->line ( 'global_active' ), '0' => $this->lang->line ( 'global_pending' ) );

        $this->_view ( "myuser/userAdd.php" );
    }
    
    protected function _addExecute() {
        if ($this->input->post ( 'kirim' ) == 'kirim') {
            $this->load->library ( 'validation' );
            $this->validation->set_error_delimiters ( );
            $rules ['username'] = "required|trim|min_length[5]|max_length[20]";
            $rules ['password'] = "required|trim|min_length[5]";
            $rules ['name'] = "required|trim";
            $rules ['status'] = "required";
            
            $data = array (
                'userLogin' => trim ( $this->input->post ( 'username' ) ), 
                'userPassword' => trim ( $this->input->post ( 'password' ) ), 
                'userName' => trim ( $this->input->post ( 'name' ) ), 
                'userStatus' => trim ( $this->input->post ( 'status' ) ) 
            );
            $this->load->model ( 'user_model' );
            $this->load->model ( 'group_model' );
            
            $result = array ();
            $result ['is_success'] = false;
            $result ['status'] = sprintf ( $this->lang->line ( 'myuser_user_add_failed' ), $data ['userLogin'] );
            
            //get members_location
            //$members_location = members_get_cfg ( 'members_location' );
            $this->validation->set_rules ( $rules );
            if ($this->validation->run () == false) {
                $result ['status'] = $this->validation->error_string;
            } elseif ($this->user_model->getByLogin ( $data ['userLogin'] )) {
                $result ['status'] = 'Username already exists';
            } elseif ($this->user_model->add ($data)) {
                $result ['is_success'] = true;
                $userId = $this->db->insert_id ();
                $this->_data['userId'] = $userId;
                
                $result ['id'] = $userId;
                $result ['status'] = sprintf ( $this->lang->line ( 'myuser_user_add_success' ), anchor ( 'myuser/user/detail/' . $result ['id'], $data ['userLogin'] ), $data ['userPassword'] );
                
                //Add Location & Access Location (Rahmat)
                $parentUserId = getUserId();
        		$dataLocation = getUserLocations($parentUserId);
        		$this->user_model->updateUserLocation($userId,$dataLocation);
        		
        		$dataAccessLocation = getUserAccessLocation($parentUserId);
                $this->user_model->updateConfig($userId,'userAccessLocation',$dataAccessLocation);
                //End
                
                //$group_default = get_cfg('group_default');
                //if ($members_location)
                    //members_update_cfg ( 'members_location', $members_location, $result ['id'] );
                /* if ($group_default)
                    foreach($group_default as $members_group_id) {
                        $this->group_model->add_members($result['id'],$members_group_id);
                    }
                */
            }
            //$this->mylogs->log ( 'myuser', 'members', 'add', $result ['is_success'], $result ['status'], array ('members_id' => $result ['id'], 'members_username' => $data ['username'], 'members_data' => $data, 'username' => $this->myindo_members->getUsername () ), $this->myindo_members->getId () );
            if (isAjax ()) {
                //$this->load->helper('myjson');
                //myjson_header();
                //echo myjson_encode($result,true);
                echo Zend_Json::encode ( $result );
            } else {
                $this->_data ['errorMessage'] = $result ['status'];
                $this->_data ['isSuccess'] = $result ['is_success'];
            }
        }
    }
    
    function edit() {
        if (! isAccess ( 'myuser', 'user', 'edit' )) {
            redirect ( 'myuser/user/view' );
        }        
        $array = $this->uri->uri_to_assoc(3,array('id','time'));
        $id = $array['id'];
        if (!$id)
            show_404();
        //if (! isAccess ( 'myuser_user_edit', 'Members Edit' ) || ! $id)
        //    show_404 ();
        add_title ( $this->lang->line ( 'myuser_user_update_title' ) );
        $this->load->model ( 'user_model' );
        $userDetail = $this->user_model->get ( $id );
        if (! $userDetail)
            show_404 ();
        $this->_executeEdit ( $userDetail );
        
        //load status
        //$this->load->model('status_model');
        //tpl_assign('status_radios',$this->status_model->get_list());
        

        $this->_data ['dataEdit'] = $userDetail;
        $this->_view ( "myuser/userEdit.php" );
    }
    
    protected function _executeEdit($userDetail) {
        if ($this->input->post ( 'kirim' ) == 'kirim') {
            $this->load->library ( 'validation' );
            $this->validation->set_error_delimiters ( '<div class="error">', '</div>' );
            $rules ['username'] = "required|trim|min_length[5]|max_length[20]";
            $rules ['name'] = "required|trim";
            $rules ['status'] = "required";
            
            $data = array (
                'userLogin' => trim ( $this->input->post ( 'username' ) ), 
                'userName' => trim ( $this->input->post ( 'name' ) ), 
                'userStatus' => trim ( $this->input->post ( 'status' ) ) 
            );
            $this->load->model ( 'user_model' );
            $this->load->model ( 'group_model' );
            
            $result = array ();
            $result ['is_success'] = false;
            $result ['status'] = sprintf ( $this->lang->line ( 'myuser_user_update_failed' ), $data ['userLogin'] );
            
            //get members_location
            //$members_location = members_get_cfg ( 'members_location' );
            $this->validation->set_rules ( $rules );
            if ($this->validation->run () == false) {
                $result ['status'] = $this->validation->error_string;
            } elseif ($this->user_model->update ($userDetail->userId,$data)) {
                $result ['is_success'] = true;
                $result ['id'] = $userDetail->userId;
                $result ['status'] = sprintf ( $this->lang->line ( 'myuser_user_update_success' ), anchor ( 'myuser/user/detail/id/' . $result ['id'] . '/time/' . time(), $data ['userLogin'] ) );
            }
            //$this->mylogs->log ( 'myuser', 'members', 'add', $result ['is_success'], $result ['status'], array ('members_id' => $result ['id'], 'members_username' => $data ['username'], 'members_data' => $data, 'username' => $this->myindo_members->getUsername () ), $this->myindo_members->getId () );
            if (isAjax ()) {
                //$this->load->helper('myjson');
                //myjson_header();
                //echo myjson_encode($result,true);
                echo Zend_Json::encode ( $result );
            } else {
                $this->_data ['errorMessage'] = $result ['status'];
                $this->_data ['isSuccess'] = $result ['is_success'];
            }
        }
    }

    public function resetPasswd() {
        if (! isAccess ( 'myuser', 'user', 'resetpasswd' )) {
            redirect ( 'myuser/user/view' );
        }        
        $array = $this->uri->uri_to_assoc(3,array('id','time'));
        $id = $array['id'];
        if (!$id)
            show_404();
        //if (! isAccess ( 'myuser_user_resetpasswd', 'Members Reset Password' ) || ! $id)
        //    show_404 ();
        add_js_plugin ( 'jquery.password.min.js' );
        add_title ( $this->lang->line ( 'myuser_user_resetpasswd_title' ) );
        $this->load->model ( 'user_model' );
        $userDetail = $this->user_model->get ( $id );
        if (! $userDetail)
            show_404 ();
        $this->_executeResetPasswd ( $userDetail );
        $this->_data ['userDetail'] = $userDetail;
        $this->_view ( "myuser/userResetPasswd.php" );
    }
    
    protected function _executeResetPasswd($userDetail) {
        if ($this->input->post ( 'kirim' ) == 'kirim') {
            $this->load->library ( 'validation' );
            $this->validation->set_error_delimiters ( '', '' );
            //$rules ['password'] = "required|trim|min_length[6]";
            $rules ['password'] = "required|trim";
            
            $data = array (
                'userPassword' => trim ( $this->input->post ( 'password' ) )
            );
            $this->load->model ( 'user_model' );
            $this->load->model ( 'group_model' );
            
            $result = array ();
            $result ['is_success'] = false;
            $result ['status'] = sprintf ( $this->lang->line ( 'myuser_user_resetpasswd_failed' ), $userDetail->userLogin );            
            $this->validation->set_rules ( $rules );
            if ($this->validation->run () == false) {
                $result ['status'] = $this->validation->error_string;
            } elseif ($this->user_model->update ($userDetail->userId,$data)) {
                $result ['is_success'] = true;
                $result ['id'] = $userDetail->userId;
                $result ['status'] = sprintf ( $this->lang->line ( 'myuser_user_resetpasswd_success' ), anchor ( 'myuser/user/detail/id/' . $userDetail->userId, $userDetail->userLogin ), $data ['userPassword'] );                
            }
            //$this->mylogs->log ( 'myuser', 'members', 'add', $result ['is_success'], $result ['status'], array ('members_id' => $result ['id'], 'members_username' => $data ['username'], 'members_data' => $data, 'username' => $this->myindo_members->getUsername () ), $this->myindo_members->getId () );
            if (isAjax ()) {
                //$this->load->helper('myjson');
                //myjson_header();
                //echo myjson_encode($result,true);
                echo Zend_Json::encode ( $result );
            } else {
                $this->_data ['errorMessage'] = $result ['status'];
                $this->_data ['isSuccess'] = $result ['is_success'];
            }
        }
    }

    protected function _getUserAccess($parentAccessLocation) {
    	
    	$where = array();
        if( empty($parentAccessLocation['kotama']) ) {
        	$where['kotamaId'] = '';
        } elseif( $parentAccessLocation['kotama'] != 'x' ) {
       		$where['kotamaId'] = $parentAccessLocation['kotama'];
       		
	        if( empty($parentAccessLocation['satuan']) ) {
	        	$where['satuanId'] = '';
	        } elseif( $parentAccessLocation['satuan'] != 'x' ) {
	       		$where['satuanId'] = $parentAccessLocation['satuan'];
	       		
	       		if( empty($parentAccessLocation['unit']) ) {
	        	$where['unitId'] = '';
		        } elseif( $parentAccessLocation['unit'] != 'x' ) {
		       		$where['unitId'] = $parentAccessLocation['unit'];
		       		
		       		if( empty($parentAccessLocation['bagian']) ) {
			        	$where['bagianId'] = '';
			        } elseif( $parentAccessLocation['bagian'] != 'x' ) {
			       		$where['bagianId'] = $parentAccessLocation['bagian'];
			       		
			       		if( empty($parentAccessLocation['subBagian']) ) {
				        	$where['subBagianId'] = '';
				        } elseif( $parentAccessLocation['subBagian'] != 'x' ) {
				       		$where['subBagianId'] = $parentAccessLocation['subBagian'];
				        }  
			        }  
		        }  
	        }        
        }
    	return $where;
    }
    
    function detail() {
        if (! isAccess ( 'myuser','user','detail' ))
            redirect ( 'myuser/user/view' );
        add_title ( $this->lang->line ( 'myuser_user_detail_title' ) );
        $array = $this->uri->uri_to_assoc(3,array('id','time','action'));
        $id = $array['id'];
        if (! $id)
            show_404 ();
            
        $this->load->model ( 'user_model' );
        
        //Akses From User Logged
        $parentId = getUserId();
        $parentAccessLocation = getUserAccessLocation($parentId);
        
        $where = $this->_getUserAccess($parentAccessLocation);
        $userDetail = $this->user_model->get ( $id , $where );
        if (! $userDetail)
            show_404 ();
        if (isset($array['action']) && $array['action'] == 'resetSession' && isAccess ( 'myuser','user','resetsession' )) {
            if ($this->user_model->updateUserIdentify($id,array('userTimeAccess'=>0))) {
                $this->_data['errorMessage'] = 'Berhasil Reset Session';
                $this->_data['isSuccess'] = TRUE;
            } else {
                $this->_data['errorMessage'] = 'Gagal Reset Session';
            }
            if ($id == getUserId())
                redirect ( 'myuser/user/login' );
            $userDetail = $this->user_model->get ( $id , $where );
        }
        $this->_data ['userDetail'] = $userDetail;
        
        $userGroup = $this->user_model->getUserGroup ( $id );
        $this->_data ['userGroup'] = $userGroup;
        
        //execute _add_members
        $this->_addAccess ( $id );
        $this->_addGroup ( $id );
        $this->_delGroup ( $id );

        //load list members_access
        $list_access = $this->myindo_user->getListAccess ();
        $this->_data ['listAccess'] = $list_access;
        
        //load list members_access_to_members
        $list_user_access = $this->user_model->getListAccess ( $id );
        $this->_data ['listUserAccess'] = $list_user_access;
        
        //load group list
        $this->load->model ( 'group_model' );
        $group_list = $this->group_model->getList ();
        $group_list_myOptions = array ();
        foreach ( $group_list as $key => $value )
            $group_list_myOptions [$value['groupId']] = $value['groupName'];
        $this->_data ['groupListMyOptions'] = $group_list_myOptions;
        
        //load members_group_to_members
        $members_group_list = $this->user_model->getListGroup ( $id, true );
        $this->_data ['membersGroupList'] = $members_group_list;
		
        $this->_data['uri_to_assoc'] = $array;
        $this->_view ( "myuser/userDetail" );
    }
    protected function _userLocation($userId) {
        if ($this->input->post ( 'submitLocation' ) && $this->input->post('submitLocation') != '' && $this->input->post ( 'kirim' ) == 'userDetailLocation') {
            $data = array(
                'kotamaId'=>'',
                'satuanId'=>'',
                'unitId'=>'',
                'bagianId'=>'',
            	'subBagianId'=>''
            );
            $data['kotamaId'] = $this->input->post('kotama');
            if ($this->input->post('kotama') && $this->input->post('satuan')) {
                $data['satuanId'] = $this->input->post('satuan');
                if ($this->input->post('kotama') && $this->input->post('satuan') && $this->input->post('unit')) {
                    $data['unitId'] = $this->input->post('unit');
                    if ($this->input->post('kotama') && $this->input->post('satuan') && $this->input->post('unit') && $this->input->post('bagian')) {
                        $data['bagianId'] = $this->input->post('bagian');
                    	if ($this->input->post('kotama') && $this->input->post('satuan') && $this->input->post('unit') && $this->input->post('bagian') && $this->input->post('subBagian')) {
                        	$data['subBagianId'] = $this->input->post('subBagian');
                    	}
                    }
                }
            }

            if ($this->user_model->updateUserLocation($userId,$data)) {
                $this->_data['errorMessage'] = 'Berhasil Update Lokasi';
                $this->_data['isSuccess'] = true;
            } else {
                $this->_data['errorMessage'] = 'Gagal Update Lokasi';
            }
        }
    }
    protected function _userAccessLocation($userId) {
        if ($this->input->post ( 'submitAccessLocation' ) && $this->input->post('submitAccessLocation') != '' && $this->input->post ( 'kirim' ) == 'userDetailAccessLocation') {
            $data = array(
                'kotama'=>'',
                'satuan'=>'',
                'unit'=>'',
                'bagian'=>'',
            	'subBagian'=>''
            );
            $data['kotama'] = $this->input->post('kotamaAccess');
            if ($this->input->post('kotamaAccess') && $this->input->post('satuanAccess')) {
                $data['satuan'] = $this->input->post('satuanAccess');
                if ($this->input->post('kotamaAccess') && $this->input->post('satuanAccess') && $this->input->post('unitAccess')) {
                    $data['unit'] = $this->input->post('unitAccess');
                    if ($this->input->post('kotamaAccess') && $this->input->post('satuanAccess') && $this->input->post('unitAccess') && $this->input->post('bagianAccess')) {
                        $data['bagian'] = $this->input->post('bagianAccess');
                    	if ($this->input->post('kotamaAccess') && $this->input->post('satuanAccess') && $this->input->post('unitAccess') && $this->input->post('bagianAccess') && $this->input->post('subBagianAccess')) {
                        	$data['subBagian'] = $this->input->post('subBagianAccess');
                    	}
                    }
                }
            }
            if ($this->user_model->updateConfig($userId,'userAccessLocation',$data)) {
                $this->_data['errorMessage'] = 'Berhasil Update Access Lokasi';
                $this->_data['isSuccess'] = true;
            } else {
                $this->_data['errorMessage'] = 'Gagal Update Access Lokasi';
            }
        }
    }    
    function _addAccess($id) {
        if ($this->input->post ( 'kirim' ) == 'userDetailAccess' && isAccess ( 'myuser', 'user','access' )) {
            $userAccess = $this->input->post ( 'access' );
            if ($this->user_model->addAccess ( $id, $userAccess )) {
                $this->_data ['errorMessage'] = 'Berhasil Update Access';
                $this->_data ['isSuccess'] = true;
                //$this->mylogs->log ( 'myuser', 'members', 'addAccess', 1, $this->_data ['errorMessage'], array ('members_id' => $id, 'username' => $this->myindo_members->getUsername () ), $this->myindo_members->getId () );
            } else {
                $this->_data ['errorMessage'] = 'Gagal Update Access';
                //$this->mylogs->log ( 'myuser', 'members', 'addAccess', 0, $this->_data ['errorMessage'], array ('members_id' => $id, 'username' => $this->myindo_members->getUsername () ), $this->myindo_members->getId () );
            }
        }
    
    }
    
    protected function _addGroup($id) {
        if ($this->input->post ( 'kirim' ) == 'members_group_add') {
            $this->load->model ( 'group_model' );
            $members_group_id = $this->input->post ( 'members_group_id' );
            if ($this->group_model->addMembers ( $id, $members_group_id )) {
                $this->_data ['errorMessage'] = 'Berhasil Tambah Group';
                $this->mylogs->log ( 'myuser', 'members', 'addGroup', 1, $this->_data ['errorMessage'], array ('members_id' => $id, 'username' => $this->myindo_members->getUsername () ), $this->myindo_members->getId () );
            } else {
                $this->_data ['errorMessage'] = 'Gagal Tambah Group';
                $this->mylogs->log ( 'myuser', 'members', 'addGroup', 0, $this->_data ['errorMessage'], array ('members_id' => $id, 'username' => $this->myindo_members->getUsername () ), $this->myindo_members->getId () );
            }
        }
    }
    
    protected function _delGroup($id) {
        if ($this->input->post ( 'kirim' ) == 'members_group_del') {
            $this->load->model ( 'group_model' );
            $members_group_id = $this->input->post ( 'members_group_id' );
            if ($this->group_model->delMembers ( $id, $members_group_id )) {
                $this->_data ['errorMessage'] = 'Berhasil Delete Group';
                $this->mylogs->log ( 'myuser', 'members', 'delGroup', 1, $this->_data ['errorMessage'], array ('members_id' => $id, 'username' => $this->myindo_members->getUsername () ), $this->myindo_members->getId () );
            } else {
                $this->_data ['errorMessage'] = 'Gagal Delete Group';
                $this->mylogs->log ( 'myuser', 'members', 'delGroup', 0, $this->_data ['errorMessage'], array ('members_id' => $id, 'username' => $this->myindo_members->getUsername () ), $this->myindo_members->getId () );
            }
        }
    }
    
    
    /*
    protected function _executeChPasswd() {
        if ($this->input->post ( 'kirim' ) == 'chpasswd') {
            $oldPasswd = trim ( $this->input->post ( 'oldPasswd' ) );
            $newPasswd = trim ( $this->input->post ( 'newPasswd' ) );
            $newPasswdConf = trim ( $this->input->post ( 'newPasswdConf' ) );
            $errors = array ();
            if (empty ( $oldPasswd ))
                $errors [] = 'Fill Old Password';
            if (empty ( $newPasswd ))
                $errors [] = 'Fill New Password';
            if (empty ( $newPasswdConf ))
                $errors [] = 'Retype New Password';
            if (! (empty ( $newPasswd ) || empty ( $newPasswdConf ))) {
                if ($newPasswd != $newPasswdConf)
                    $errors [] = 'New Password not match';
                elseif ($newPasswd == $oldPasswd || strpos ( $newPasswd, $oldPasswd ) !== false)
                    $errors [] = 'Password cant same';
            }
            if (! empty ( $newPasswd ) && strlen ( $newPasswd ) < 8)
                $errors [] = 'You must enter a minimum of 8 characters.';
            if ($errors) {
                $this->_data ['errorMessage'] = implode ( '<br />' . PHP_EOL, $errors );
            } elseif ($this->myindo_members->chpasswd ( $oldPasswd, $newPasswd ) === MEMBERS_CHPASSWD_SUCCESS) {
                $this->_data ['errorMessage'] = 'Success Change Password. Please Login Again.';
                $this->_data ['isSuccess'] = true;
                $this->myindo_members->logout ();
                header ( 'Refresh:5;url=' . site_url ( 'myuser/login' ) );
            } else {
                $this->_data ['errorMessage'] = 'Failed Change Password';
            }
            $this->mylogs->log ( 'myuser', 'members', 'chPasswd', $this->_data ['isSuccess'], $this->_data ['errorMessage'], array ('members_id' => $this->myindo_members->getId (), 'username' => $this->myindo_members->getUsername () ), $this->myindo_members->getId () );
        }
    }

    */

    protected function _executeProfile($userDetail) {
        if ($this->input->post ( 'kirim' ) == 'kirim') {
            $this->load->library ( 'validation' );
            $this->validation->set_error_delimiters ( '', '<br />' );
			
            $data = array ();
            $data['userName'] = trim ( $this->input->post ( 'name' ) );
            $change = $this->input->post('changePassword');
            
            $rules ['name'] = "required|trim";
            if($change == '1') {
	            $rules ['old_password'] = "required|trim";
	            $rules ['password'] = "required|trim";
	            $rules ['password_conf'] = "required|trim|matches[password]";
	            
	            $data['userPassword'] = trim ( $this->input->post ( 'password' ) );
            }
           
            $this->load->model ( 'user_model' );
            $this->load->model ( 'group_model' );
            
            $result = array ();
            $result ['is_success'] = false;
            $result ['status'] = sprintf ( $this->lang->line ( 'myuser_user_update_failed' ), $this->input->post ( 'name' ) );
            
            //get members_location
            //$members_location = members_get_cfg ( 'members_location' );
            $this->validation->set_rules ( $rules );
            
            if ($this->validation->run () == false) {
                $result ['status'] = $this->validation->error_string;
            } else {
            	
            	$dataUser = $this->user_model->getUser ($userDetail->userId);
            	$oldPassword = $this->input->post('old_password');
            	
            	if( crypt($oldPassword,$dataUser['userPassword']) != $dataUser['userPassword'] && $change == '1') {
            		$result ['status'] = "Password Lama Tidak Valid";
            	} else {
            		$this->user_model->update ($userDetail->userId,$data);
                	$result ['is_success'] = true;
                	$result ['id'] = $userDetail->userId;
                	$result ['status'] = sprintf ( $this->lang->line ( 'myuser_user_update_success' ), anchor ( 'myuser/user/detail/id/' . $result ['id'] . '/time/' . time(), $this->input->post ( 'name' ) ) );
            	}
            }
            //$this->mylogs->log ( 'myuser', 'members', 'add', $result ['is_success'], $result ['status'], array ('members_id' => $result ['id'], 'members_username' => $data ['username'], 'members_data' => $data, 'username' => $this->myindo_members->getUsername () ), $this->myindo_members->getId () );
            if (isAjax ()) {
                //$this->load->helper('myjson');
                //myjson_header();
                //echo myjson_encode($result,true);
                echo Zend_Json::encode ( $result );
            } else {
                $this->_data ['errorMessage'] = $result ['status'];
                $this->_data ['isSuccess'] = $result ['is_success'];
            }
        }
    }    
    
    public function profile() {
        $id = getUserId();
        if (!$id)
            show_404();
        //if (! isAccess ( 'myuser_user_edit', 'Members Edit' ) || ! $id)
        //    show_404 ();
        add_title ( $this->lang->line ( 'myuser_user_update_title' ) );
        $this->load->model ( 'user_model' );
        $userDetail = $this->user_model->get ( $id );
        if (! $userDetail)
            show_404 ();
        $this->_executeProfile ( $userDetail );
        
        //load status
        //$this->load->model('status_model');
        //tpl_assign('status_radios',$this->status_model->get_list());
        

        $this->_data ['dataEdit'] = $userDetail;
        $this->_view ( "myuser/userProfile.php" );
    }
    public function online() {

        $array = $this->uri->uri_to_assoc(3,array('page'));
        if (!$array['page'])
            $array['page'] = 0;
        add_title('User Online');
        $this->load->module_model('myuser','user_model');
        $total = $this->user_model->getCountUserOnline();
        $limit = $this->config->item ( 'user_limit' );
        $this->load->library ( 'pagination' );
        $config = $this->config->item('paging');
        $config ['base_url'] = site_url ( 'myuser/user/online/page' );
        $config ['total_rows'] = $total;
        $config ['per_page'] = $limit;
        $config ['uri_segment'] = 4 ;
        $this->pagination->initialize ( $config );
        $pages_html = $this->pagination->create_links ();
        $this->_data ['pages_html'] = $pages_html;
        $listOnline = $this->user_model->getListUserOnline($limit,$array['page']);
        $this->_data['listOnline'] = $listOnline;
                
        $this->_view ( "myuser/userOnline.php" );        
    }    
    /*
    protected function _executeChPasswdOld($randomid=0)
    {
        if (! $randomid) show_404();
        if ($this->input->post('kirim') == 'kirim') {
            //load helper,library and model
            $this->load->helper('myjson');
            $this->load->library('validation');
            $this->load->model('myuser_members');

            //rule
            $rules['oldpasswd'] = 'required|trim';
            $rules['passwd'] = 'required|trim|min_length[6]|max_length[20]|matches[passwdconf]';
            $rules['passwdconf'] = 'required|trim';
            $this->validation->set_rules($rules);
            $this->validation->set_error_delimiters('<div class="error">', '</div>');

            //data status
            $data['status'] = $this->lang->line('myuser_user_chpasswd_failed');
            $data['is_success'] = false;
            $oldpasswd = trim($this->input->post('oldpasswd'));
            $passwd = trim($this->input->post('passwd'));
            $passwdconf = trim($this->input->post('passwdconf'));

            if ($this->validation->run() == FALSE) {
                $data['status'] = $this->validation->error_string;
            } elseif ($passwd <> $passwdconf) {
                $data['status'] = $this->lang->line('myuser_user_chpasswd_retype');
            } else {
                $status = $this->myindo->members->chpasswd($oldpasswd,$passwd);
                switch($status) {
                    case MEMBERS_CHPASSWD_FAILED:
                        break;
                    case MEMBERS_CHPASSWD_SUCCESS:
                        $data['is_success'] = true;
                        $this->myuser_members->update_config($this->myindo->members->get_id(),'myuser_user_forcepasswd',0);
                        $this->myindo->members->logout();
                        $data['status'] = $this->lang->line('myuser_user_chpasswd_success');
                        break;
                    case MEMBERS_CHPASSWD_NOTLOGIN :
                        $data['status'] = $this->lang->line('myuser_user_chpasswd_expire');
                        break;
                    case MEMBERS_CHPASSWD_WRONGPASSWD :
                        $data['status'] = $this->lang->line('myuser_user_chpasswd_wrongpass');
                        break;
                }
            }
            myjson_header();
            echo myjson_encode($data,true);
        }
    }
    */
    public function logs() {
        if (! isAccess ( 'myuser_user_log', 'Members Log' ))
            redirect ( '' );
        $array = $this->uri->uri_to_assoc ( 3, array ('page' ) );
        $array ['page'] = ( int ) $array ['page'];
        
        $where = array ();
        $limit = $this->config->item ( 'myuser_limit' );
        $this->_data ['uri_to_assoc'] = $array;
        $total = $this->mylogs->getCount ( $where );
        $this->_data ['listLogs'] = $this->mylogs->getList ( $where, $limit, $array ['page'] );
        
        unset ( $array ['page'] );
        $this->load->library ( 'pagination' );
        $config ['base_url'] = site_url ( 'myuser/members/logs/' . $this->uri->assoc_to_uri ( $array ) . '/page' );
        $config ['total_rows'] = $total;
        $config ['per_page'] = $limit;
        $config ['uri_segment'] = 4 + (2 * count ( $array ));
        $this->pagination->initialize ( $config );
        $pages_html = $this->pagination->create_links ();
        $this->_data ['pages_html'] = $pages_html;
        
        $this->_view ( 'myuser/membersLogs' );
    }
    
    /*
    function detail() {
        if (! isAccess ( 'myuser','user','detail' ))
            redirect ( 'myuser/user/view' );
        add_title ( $this->lang->line ( 'myuser_user_detail_title' ) );
        $array = $this->uri->uri_to_assoc(3,array('id','time','action'));
        $id = $array['id'];
        if (! $id)
            show_404 ();
            
        $this->load->model ( 'user_model' );
        
        //Akses From User Logged
        $parentId = getUserId();
        $parentAccessLocation = getUserAccessLocation($parentId);
        
        $where = $this->_getUserAccess($parentAccessLocation);
        $userDetail = $this->user_model->get ( $id , $where );
        if (! $userDetail)
            show_404 ();
        if (isset($array['action']) && $array['action'] == 'resetSession' && isAccess ( 'myuser','user','resetsession' )) {
            if ($this->user_model->updateUserIdentify($id,array('userTimeAccess'=>0))) {
                $this->_data['errorMessage'] = 'Berhasil Reset Session';
                $this->_data['isSuccess'] = TRUE;
            } else {
                $this->_data['errorMessage'] = 'Gagal Reset Session';
            }
            if ($id == getUserId())
                redirect ( 'myuser/user/login' );
            $userDetail = $this->user_model->get ( $id , $where );
        }
        $this->_data ['userDetail'] = $userDetail;
        
        $userGroup = $this->user_model->getUserGroup ( $id );
        $this->_data ['userGroup'] = $userGroup;
        
        //execute _add_members
        $this->_addAccess ( $id );
        $this->_addGroup ( $id );
        $this->_delGroup ( $id );
        //$this->_updateLocation($id);
        

        //load list members_access
        $list_access = $this->myindo_user->getListAccess ();
        //assign list_access
        $this->_data ['listAccess'] = $list_access;
        
        //load list members_access_to_members
        $list_user_access = $this->user_model->getListAccess ( $id );
        //assign list_user_access
        $this->_data ['listUserAccess'] = $list_user_access;
        
        //load group list
        $this->load->model ( 'group_model' );
        $group_list = $this->group_model->getList ();
        $group_list_myOptions = array ();
        foreach ( $group_list as $key => $value )
            $group_list_myOptions [$value['groupId']] = $value['groupName'];
        $this->_data ['groupListMyOptions'] = $group_list_myOptions;
        
        //load members_group_to_members
        $members_group_list = $this->user_model->getListGroup ( $id, true );
        $this->_data ['membersGroupList'] = $members_group_list;

        //Lokasi	
        $this->_userLocation($id);
        $userLocation = $this->user_model->getConfigLocation($id);
        
		$this->_data['kotama'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>0),true);
        $this->_data['satuanKerja'] = array();
        $this->_data['unitKerja'] = array();
        $this->_data['bagian'] = array();
		$this->_data['subBagian'] = array();
		
        //Kotama
        if($parentAccessLocation['kotama'] == 'x') {
	        if ($userLocation['kotama']) {
	            $this->_data['satuanKerja'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['kotama']),true);
	            if ($userLocation['satuan']) {
	                $this->_data['unitKerja'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['satuan']),true);
	                if ($userLocation['unit']) {
	                    $this->_data['bagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['unit']),true);
	                	if ($userLocation['bagian']) {
	                    	$this->_data['subBagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['bagian']),true);
	                	}
	                }
	            }
	        }
	        if ($this->input->post ( 'kirim' ) == 'userDetailLocation') {
	            if ($this->input->post('kotama') !== false) {
	                $userLocation['kotama'] = $this->input->post('kotama');
	                $this->_data['satuanKerja'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('kotama')),true);
	                if ($this->input->post('satuan') !== false) {
	                    $userLocation['satuan'] = $this->input->post('satuan');
	                    $this->_data['unitKerja'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('satuan')),true);
	                    if ($this->input->post('unit') !== false) {
	                        $userLocation['unit'] = $this->input->post('unit');
	                        $this->_data['bagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('unit')),true);
	                    	if ($this->input->post('bagian') !== false) {
	                        	$userLocation['bagian'] = $this->input->post('bagian');
	                        	$this->_data['subBagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('bagian')),true);
	                    	}
	                    }
	                }
	                
	            }
	        }
	        
        } elseif($parentAccessLocation['kotama']) { 
        	$userLocation['kotama'] = $parentAccessLocation['kotama'];
	    	$this->_data['satuanKerja'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['kotama']),true);
	    	
	    	//Satuan
	    	if($parentAccessLocation['satuan'] == 'x') {
	            if ($userLocation['satuan']) {
	                $this->_data['unitKerja'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['satuan']),true);
	                if ($userLocation['unit']) {
	                    $this->_data['bagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['unit']),true);
	                	if ($userLocation['bagian']) {
	                    	$this->_data['subBagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['bagian']),true);
	                	}
	                }
	            }
		       
		        if ($this->input->post ( 'kirim' ) == 'userDetailLocation') {
	                if ($this->input->post('satuan') !== false) {
	                    $userLocation['satuan'] = $this->input->post('satuan');
	                    $this->_data['unitKerja'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('satuan')),true);
	                    if ($this->input->post('unit') !== false) {
	                        $userLocation['unit'] = $this->input->post('unit');
	                        $this->_data['bagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('unit')),true);
	                    	if ($this->input->post('bagian') !== false) {
	                        	$userLocation['bagian'] = $this->input->post('bagian');
	                        	$this->_data['subBagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('bagian')),true);
	                    	}
	                    }
	                }  
		        }
		        
	    	} elseif($parentAccessLocation['satuan']) { 
	    		$userLocation['satuan'] = $parentAccessLocation['satuan'];
	    		$this->_data['unitKerja'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['satuan']),true);
	    		
	    		//Unit
		    	if($parentAccessLocation['unit'] == 'x') {
					if ($userLocation['unit']) {
	                    $this->_data['bagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['unit']),true);
	                	if ($userLocation['bagian']) {
	                    	$this->_data['subBagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['bagian']),true);
	                	}
	                }
			       
			        if ($this->input->post ( 'kirim' ) == 'userDetailLocation') {
	                    if ($this->input->post('unit') !== false) {
	                        $userLocation['unit'] = $this->input->post('unit');
	                        $this->_data['bagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('unit')),true);
	                    	if ($this->input->post('bagian') !== false) {
	                        	$userLocation['bagian'] = $this->input->post('bagian');
	                        	$this->_data['subBagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('bagian')),true);
	                    	}
	                    }
			        }
			        
		    	} elseif($parentAccessLocation['unit']) { 
		    		$userLocation['unit'] = $parentAccessLocation['unit'];
		    		$this->_data['bagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['unit']),true);
		    	
		    		//Bagian
		    		if($parentAccessLocation['bagian'] == 'x') {
	                	if ($userLocation['bagian']) {
	                    	$this->_data['subBagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['bagian']),true);
	                	}

				        if ($this->input->post ( 'kirim' ) == 'userDetailLocation') {
	                    	if ($this->input->post('bagian') !== false) {
	                        	$userLocation['bagian'] = $this->input->post('bagian');
	                        	$this->_data['subBagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('bagian')),true);
	                    	}
				        }
				        
		    		} elseif($parentAccessLocation['bagian']) {
		    			$userLocation['bagian'] = $parentAccessLocation['bagian'];
		    			$this->_data['subBagian'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userLocation['bagian']),true);
		    			
		    			//Sub Bagian
		    			if($parentAccessLocation['subBagian'] == 'x') {
		    				
		    			} elseif($parentAccessLocation['subBagian']) {
		    				$userLocation['subBagian'] = $parentAccessLocation['subBagian'];
		    			} else {
		    				$this->_data['subBagian'] = array();
		    			}
		    			//End Sub Bagian
		    			
		    		} else {
		    			$this->_data['bagian'] = array();
		    		}
		    		//End Bagian
		    		
		    	} else {
		    		$this->_data['unitKerja'] = array();
		    	}
		    	//End Unit
	    		
	    	} else {
	    		$this->_data['satuanKerja'] = array();
	    	}
	    	//End Satuan
	    	
        } else {
    		$this->_data['kotama'] = array();
    	}
        //End Kotama
        
        $this->_data['userLocation'] = $userLocation;
        //End Lokasi
        
        //Akses Lokasi	
        $this->_userAccessLocation($id);
        $userAccessLocation = $this->user_model->getConfig($id,'userAccessLocation');
        if (!$userAccessLocation) {
            $userAccessLocation = array('kotama'=>'','satuan'=>'','unit'=>'','bagian'=>'','subBagian'=>'');
            $this->user_model->updateConfig($id,'userAccessLocation',$userAccessLocation);
        }
        
        $this->_data['kotamaAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>0),true);
        $this->_data['satuanKerjaAccess'] = array();
        $this->_data['unitKerjaAccess'] = array();
        $this->_data['bagianAccess'] = array();
        $this->_data['subBagianAccess'] = array();
		
    	//Kotama
        if($parentAccessLocation['kotama'] == 'x') {
	        if ($userAccessLocation['kotama']) {
	            $this->_data['satuanKerjaAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['kotama']),true);
	            
	            if ($userAccessLocation['satuan']) {
	                $this->_data['unitKerjaAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['satuan']),true);
	                
	                if ($userAccessLocation['unit']) {
	                    $this->_data['bagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['unit']),true);
	                	
	                    if ($userAccessLocation['bagian']) {
	                    	$this->_data['subBagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['bagian']),true);
	                	}
	                }
	            }
	        }
	        if ($this->input->post ( 'kirim' ) == 'userDetailAccessLocation') {
	            if ($this->input->post('kotamaAccess') !== false) {
	                $userAccessLocation['kotama'] = $this->input->post('kotamaAccess');
	                $this->_data['satuanKerjaAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('kotamaAccess')),true);
	                
	                if ($this->input->post('satuanAccess') !== false) {
	                    $userAccessLocation['satuan'] = $this->input->post('satuanAccess');
	                    $this->_data['unitKerjaAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('satuanAccess')),true);
	                    
	                    if ($this->input->post('unitAccess') !== false) {
	                        $userAccessLocation['unit'] = $this->input->post('unitAccess');
	                        $this->_data['bagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('unitAccess')),true);
	                    	
	                        if ($this->input->post('bagianAccess') !== false) {
	                        	$userAccessLocation['bagian'] = $this->input->post('bagianAccess');
	                        	$this->_data['subBagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('bagianAccess')),true);
	                    	}
	                    }
	                }
	                
	            }
	        }
	        
        } elseif($parentAccessLocation['kotama']) { 
        	$userAccessLocation['kotama'] = $parentAccessLocation['kotama'];
	    	$this->_data['satuanKerjaAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['kotama']),true);
	    	
	    	//Satuan
	    	if($parentAccessLocation['satuan'] == 'x') {
	            if ($userAccessLocation['satuan']) {
	                $this->_data['unitKerjaAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['satuan']),true);
	                if ($userAccessLocation['unit']) {
	                    $this->_data['bagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['unit']),true);
	                	if ($userAccessLocation['bagian']) {
	                    	$this->_data['subBagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['bagian']),true);
	                	}
	                }
	            }
		       
		        if ($this->input->post ( 'kirim' ) == 'userDetailAccessLocation') {
	                if ($this->input->post('satuanAccess') !== false) {
	                    $userAccessLocation['satuan'] = $this->input->post('satuanAccess');
	                    $this->_data['unitKerjaAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('satuanAccess')),true);
	                    
	                    if ($this->input->post('unitAccess') !== false) {
	                        $userAccessLocation['unit'] = $this->input->post('unitAccess');
	                        $this->_data['bagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('unitAccess')),true);
	                    	
	                        if ($this->input->post('bagianAccess') !== false) {
	                        	$userAccessLocation['bagian'] = $this->input->post('bagianAccess');
	                        	$this->_data['subBagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('bagianAccess')),true);
	                    	}
	                    }
	                }  
		        }
		        
	    	} elseif($parentAccessLocation['satuan']) { 
	    		$userAccessLocation['satuan'] = $parentAccessLocation['satuan'];
	    		$this->_data['unitKerjaAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['satuan']),true);
	    		
	    		//Unit
		    	if($parentAccessLocation['unit'] == 'x') {
					if ($userAccessLocation['unit']) {
	                    $this->_data['bagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['unit']),true);
	                	if ($userAccessLocation['bagian']) {
	                    	$this->_data['subBagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['bagian']),true);
	                	}
	                }
			       
			        if ($this->input->post ( 'kirim' ) == 'userDetailAccessLocation') {
	                    if ($this->input->post('unitAccess') !== false) {
	                        $userAccessLocation['unit'] = $this->input->post('unitAccess');
	                        $this->_data['bagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('unitAccess')),true);
	                    	if ($this->input->post('bagianAccess') !== false) {
	                        	$userAccessLocation['bagian'] = $this->input->post('bagianAccess');
	                        	$this->_data['subBagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('bagianAccess')),true);
	                    	}
	                    }
			        }
			        
		    	} elseif($parentAccessLocation['unit']) { 
		    		$userAccessLocation['unit'] = $parentAccessLocation['unit'];
		    		$this->_data['bagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['unit']),true);
		    	
		    		//Bagian
		    		if($parentAccessLocation['bagian'] == 'x') {
	                	if ($userAccessLocation['bagian']) {
	                    	$this->_data['subBagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['bagian']),true);
	                	}

				        if ($this->input->post ( 'kirim' ) == 'userDetailAccessLocation') {
	                    	if ($this->input->post('bagianAccess') !== false) {
	                        	$userAccessLocation['bagian'] = $this->input->post('bagianAccess');
	                        	$this->_data['subBagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$this->input->post('bagianAccess')),true);
	                    	}
				        }
				        
		    		} elseif($parentAccessLocation['bagian']) {
		    			$userAccessLocation['bagian'] = $parentAccessLocation['bagian'];
		    			$this->_data['subBagianAccess'] = $this->wilayahkesatuan_model->getListForm(array('wilayahKesatuanPid'=>$userAccessLocation['bagian']),true);
		    			
		    			//Sub Bagian
		    			if($parentAccessLocation['subBagian'] == 'x') {
		    				
		    			} elseif($parentAccessLocation['subBagian']) {
		    				$userAccessLocation['subBagian'] = $parentAccessLocation['subBagian'];
		    			} else {
		    				$this->_data['subBagianAccess'] = array();
		    			}
		    			//End Sub Bagian
		    			
		    		} else {
		    			$this->_data['bagianAccess'] = array();
		    		}
		    		//End Bagian
		    		
		    	} else {
		    		$this->_data['unitKerjaAccess'] = array();
		    	}
		    	//End Unit
	    		
	    	} else {
	    		$this->_data['satuanKerjaAccess'] = array();
	    	}
	    	//End Satuan
	    	
        } else {
    		$this->_data['kotamaAccess'] = array();
    	}
        //End Kotama
        
        $this->_data['userAccessLocation'] = $userAccessLocation;
        //End Akses Lokasi
        
        $this->_data['uri_to_assoc'] = $array;
        $this->_view ( "myuser/userDetail" );
    }
     */
}
